/*
 * Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
 *
 * Licensed under the Mulan PSL v2.
 * You can use this file according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of the License at
 *
 *     http://license.coscl.org.cn/MulanPSL2
 *
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
 * FOR MORE DETAILS.
 */

#pragma once

#include "highfive/H5File.hpp"

#include "DBManager/IRepository/Input/Constraint/IRBE2Repository.h"
#include "DBManager/Repository/HDF5/Base/MainlyRepositoryBase.h"

SG_DBMANAGER_HDF5_NAMESPACE_OPEN
namespace RBE2
{
    struct IDENTITY
    {
        SG::DataStructure::Common::I64  EID;
        SG::DataStructure::Common::I64  GN;      //< 主节点编号
        SG::DataStructure::Common::I64  CM;      ///< 从自由度
        SG::DataStructure::Common::I64  GM_POS;  // Start position in GM dataset
        SG::DataStructure::Common::I64  GM_LEN;  // Length in GM dataset
        SG::DataStructure::Common::Real ALPHA;   ///< 热膨胀系数
        SG::DataStructure::Common::Real TREF;    ///< 参考温度
        SG::DataStructure::Common::I64  DOMAIN_ID;
    };
    struct GM
    {
        SG::DataStructure::Common::I64  ID;
        // 默认构造函数
        GM () : ID (0)
        {}
        GM (SG::DataStructure::Common::I64 _id) : ID (_id)
        {}
    };

};  // namespace RBE2
SG_DBMANAGER_HDF5_NAMESPACE_CLOSE

template <>
inline HighFive::DataType HighFive::create_datatype<SG::DBManager::HDF5::RBE2::IDENTITY> ()
{
    HighFive::CompoundType compoundType = {
        { "EID", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
        { "GN", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
        { "CM", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
        { "GM_POS", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
        { "GM_LEN", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
        { "ALPHA", HighFive::create_datatype<SG::DataStructure::Common::Real> () },
        { "TREF", HighFive::create_datatype<SG::DataStructure::Common::Real> () },
        { "DOMAIN_ID", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
    };
    return compoundType;
}

template <>
inline HighFive::DataType HighFive::create_datatype<SG::DBManager::HDF5::RBE2::GM> ()
{
    HighFive::CompoundType compoundType = {
        { "ID", HighFive::create_datatype<SG::DataStructure::Common::I64> () }
    };
    return compoundType;
}
SG_DBMANAGER_HDF5_NAMESPACE_OPEN
class RBE2Repository : public IRBE2Repository, public MainlyRepositoryBase<SG::DataStructure::FEM::RBE2Data>
{
  public:
    explicit RBE2Repository (HDF5Database& database);

    enum DATASET : std::uint8_t
    {
        MainDataset,
        IndexDataset,
        SubDataset
    };

    /**
     * @brief   : 初始化表数据
     * @author  : wxc
     */
    void InitialDatasetInfo () override;

    /**
     * @brief   : 将缓存数据转储到文件内
     */
    void DumpWriteCahce () override;

    /**
     * @brief   : 加载文件数据至缓存
     * @param   : loadIndex
     */
    std::vector<DataType>* LoadReadCache (std::size_t loadIndex) override;

    /**
     * @brief   : 对数据进行排序
     */
    void SortData () override;

    /**
     * @brief    Get the All Data object
     *
     * @return
     * @author   wxc
     * @date     2024.12.25
     */
    std::vector<SG::DataStructure::FEM::RBE2Data> GetAllData () override;

    /**
     * @brief   : 将自己的数据转存为nastran的数据结构
     */
    void OwnToNastran (std::vector<SG::DataStructure::FEM::RBE2Data>&         own,
                       std::vector<SG::DBManager::HDF5::RBE2::IDENTITY>& _OUT IDENTITYData,
                       std::vector<SG::DBManager::HDF5::RBE2::GM>& _OUT       subData,
                       bool                                                   isSort = false);

    /**
     * @brief   : 将NASTRAN的数据转存为自己的数据结构
     * @param   : IDENTITYData
     * @param   : subData
     * @param   : own
     */
    void NastranToOwn (std::vector<SG::DBManager::HDF5::RBE2::IDENTITY>&   IDENTITYData,
                       std::vector<SG::DBManager::HDF5::RBE2::GM>&         subData,
                       std::vector<SG::DataStructure::FEM::RBE2Data>& _OUT own);
};
SG_DBMANAGER_HDF5_NAMESPACE_CLOSE
